package com.mall507.fivepeaks.activity.community;

import android.support.v4.content.ContextCompat;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.hzh.frame.ui.fragment.AbsRecyclerViewFM;
import com.hzh.frame.util.StatusBarUtil;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.mall507.comn.model.Community;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.widget.imageview.CircleImageView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 社区首页
 * @date 2019/7/30
 */
public class CommunityRFM extends AbsRecyclerViewFM<Community> {
    
    @Override
    protected int setLayoutId() {
        return R.layout.fm_rv_community_index;
    }

    @Override
    protected void bindView(View view) {
        getTitleView().addView(StatusBarUtil.createStatusBarView(getActivity(), ContextCompat.getColor(getActivity(),R.color.application_color)),0);
        getTitleView().setLeftIsShow(false);
        getTitleView().setContent("社区");
    }

    @Override
    protected int setItemLayoutId(int viewType) {
        return R.layout.item_rv_community_main;
    }

    @Override
    protected String setHttpPath() {
        return "Community/getCommunity";
    }

    @Override
    protected List<Community> handleHttpData(JSONObject response) {
        List<Community> list = new ArrayList<>();
        if(200 == response.optInt("code")){
            JSONArray jsonArray=response.optJSONArray("data");
            for(int i=0;i<jsonArray.length();i++){
                JSONObject object=jsonArray.optJSONObject(i);
                Community model=new Community();
                model.setNid(object.optString("id"));
                model.setClickType(object.optString("clickType"));
                model.setClickValue(object.optString("clickValue"));
                model.setIcon(object.optString("imageUrl"));
                model.setTitle(object.optString("title"));
                model.setDesc(object.optString("desc"));
                list.add(model);
            }
        }
        return list;
    }

    @Override
    protected void bindItemData(RecyclerViewHolder holder, int position, final Community model) {
        holder.setText(R.id.title,model.getTitle());
        holder.setText(R.id.desc,model.getDesc());
        ((CircleImageView)holder.getView(R.id.icon)).setImageURI(model.getIcon());
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (model.getNid()){
                    case "1":
                        ARouter.getInstance().build("/main/CommunityArtUI").navigation();
                        break;
                    case "2":
                        ARouter.getInstance().build("/community/CommunityAssociationUI").navigation();
                        break;
                    case "3":
                        ARouter.getInstance().build("/community/CommunityBusinessUI").navigation(); //商圈
                        break;
                    case "4":
                        ARouter.getInstance().build("/community/CommunityProblemRUI").navigation();
                        break;
                }
            }
        });
    }
}
